package org.scep.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Default extends HttpServlet
{

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		super.service(req, resp);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
	 */
	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		super.service(req, res);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		String reqPath = req.getRequestURI();
		int index = reqPath.indexOf("/",1);
		if(index>0){
			reqPath = reqPath.substring(index);
		}
		URL url = new URL("http://www.cnbeta.com"+reqPath);
		System.out.println(url.toString());
		HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
		httpConnection.setRequestMethod("GET");
		httpConnection.setRequestProperty("host", "www.cnbeta.com");
		Enumeration enumHeaderNames = req.getHeaderNames();
		while(enumHeaderNames.hasMoreElements()){
			Object obj = enumHeaderNames.nextElement();
			if(obj!=null&&!"host".equalsIgnoreCase(obj.toString())){
				httpConnection.setRequestProperty(obj.toString(), req.getHeader(obj.toString()));
			}
		}
		httpConnection.connect();
		Map<String, List<String>> headerMap = httpConnection.getHeaderFields();
		Iterator<String> iter = headerMap.keySet().iterator();
		while(iter.hasNext()){
			String name = iter.next();
			if(name==null)
				continue;
			List<String> list = headerMap.get(name);
			if(list!=null){
				boolean flag = false;
				if(resp.containsHeader(name)){
					flag = true;
				}
				for(int listi=0,listlen=list.size();listi<listlen;listi++){
					System.out.println(name+" : "+list.get(listi));
					if(flag)
						resp.setHeader(name, list.get(listi));
					else
						resp.addHeader(name, list.get(listi));
				}
			}
		}
			InputStream is = httpConnection.getInputStream();
			PrintWriter out = resp.getWriter();
			StringBuffer sb = new StringBuffer();
			int ch;
			while((ch=is.read())!=-1){
				sb.append((char)ch);
				if(sb.length()==1024){
					out.print(sb.toString());
					out.flush();
					sb.delete(0, 1024);
				}
			}
			out.print(sb.toString());
			out.flush();
			out.close();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy()
	{
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException
	{
		// Put your code here
	}

}
